/*
 * app_status_thread.c
 *
 *  Created on: 2024年6月27日
 *      Author: xhq
 */
/* include */
#include "app_thread_modules.h"
#include "util_factory.h"

//#define SHARE_TARGET_MEM_SIZE  sizeof(g_object_info)

/* define */

/* typedef */

/* global varies*/
// 获取dsp目标信息的映射地址
uint8_t *g_obj_info_pu8_mmap = NULL;

// 存放从dsp获取到的目标信息的存储空间，用于给到追踪算法使用
//uint8_t g_u8obj_info_buf[SHARE_TARGET_MEM_SIZE] = {0};

/* function */
uint32_t map_ddr_address_func()
{
    // uint8_t *l_pu8_mmap = g_obj_info_pu8_mmap;
    int fd = 0;
    uint32_t l_u32_ret = 0;
    uint64_t l_u64_share_mem_address = 0;
    uint64_t l_u64_share_mem_size = 0;

    /* 获取目标存储的DDR首地址 */
    // l_u64_share_mem_address = l_rpmsg_status_param->rpmsg_page0_status_param.obj_info_addr;
    l_u64_share_mem_address = 0xAA000000;
    l_u64_share_mem_size = sizeof(g_object_info);

    /* 防止超出共享内存 */
    if ((l_u64_share_mem_address >= 0xAA000000) && (l_u64_share_mem_address < (0xAA000000 + 0x04000000)))
    {
        fd = open("/dev/mem", O_RDWR | O_SYNC);
        if (fd == -1) 
        {
            printf("open error!\r\n");
            goto error;
        }

        /*映射文件到内存*/
        g_obj_info_pu8_mmap = mmap(NULL, l_u64_share_mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, l_u64_share_mem_address);
        if (g_obj_info_pu8_mmap == MAP_FAILED) 
        {
            close(fd);
            printf("mmap error!\r\n");
            goto error;
        }

        printf("g_obj_info_pu8_mmap:%x!\r\n", g_obj_info_pu8_mmap);

        /* 拷贝目标信息 */
        // memcpy((uint8_t *)&g_object_info, (uint8_t *)l_pu8_mmap, sizeof(g_object_info));

        /* 解除映射*/
        // if (munmap(g_obj_info_pu8_mmap, l_u64_share_mem_size) == -1) 
        // {
        //     perror("munmap");
        // }
        // close(fd);
    }

    error:
    RETURN_ERROR_CODE(l_u32_ret, __func__);
}

/*****************************************************************************/
/**
*
* 网络配置端口数据接收任务 net recv thread
*
* @param    None.
*
* @return   None
*
* @note     None.
*
******************************************************************************/
void *app_status_thread()
{
    char l_c_thread_name[17] = "app_status";
    prctl(PR_SET_NAME, l_c_thread_name);

    sleep(2);   // 2s
    map_ddr_address_func();

    while (1)
    {
        // sem_wait(&g_dsp_pro_comlete_semaphore);

        /* 获取dsp状态参数 */
        update_dsp_rpmsg_status_info();

        /* 更新dsp配置参数 */

        thread_uSleep(500000);   // 500ms
        // usleep(2000);   // 2ms
    }
    
}
/**
 * @brief 获取dsp状态信息
 * 
 * @param None ：
 * 
 * @return uint32_t ：0：正常，非0：错误代码
 * 
 * @note：
 */
uint32_t update_dsp_rpmsg_status_info()
{
	uint32_t l_u32_ret = 0;
    uint32_t l_u32_i = 0;
    char dir_name[100] = {0};
    char filename[100] = {0};
    FILE *fp_t;
    static uint32_t s_u32frame_cnt = 0;
    rpmsg_status_param_type *l_rpmsg_status_param = get_rpmsg_status_param();

    /* dsp版本号 */
    sprintf(g_device_status.dsp_version, "mmv-liar-%02d%04d%02d%02d\n", l_rpmsg_status_param->rpmsg_page0_status_param.num,
                                                                  l_rpmsg_status_param->rpmsg_page0_status_param.year,
                                                                  l_rpmsg_status_param->rpmsg_page0_status_param.month,
                                                                  l_rpmsg_status_param->rpmsg_page0_status_param.day);

    /* 调试参数 */
	g_arm_debug_param[0] = l_rpmsg_status_param->rpmsg_page0_status_param.dsp_debug_param1;
    g_arm_debug_param[1] = l_rpmsg_status_param->rpmsg_page0_status_param.dsp_debug_param2;
    g_arm_debug_param[2] = l_rpmsg_status_param->rpmsg_page0_status_param.dsp_debug_param3;
    g_arm_debug_param[3] = l_rpmsg_status_param->rpmsg_page0_status_param.dsp_debug_param4;

    // printf("g_arm_debug_param[0] = %u, g_arm_debug_param[1] = %u, g_arm_debug_param[2] = %u, g_arm_debug_param[3] = %u,\n", 
    //     g_arm_debug_param[0], g_arm_debug_param[1], g_arm_debug_param[2], g_arm_debug_param[3]);
    
    /* 保存dsp返回的目标信息数据(这里copy大小必须为1024整数倍) */
    // memcpy(g_u8obj_info_buf, (void *)l_object_info, SHARE_TARGET_MEM_SIZE);
    
    /* 通知追踪算法线程进行目标轨迹信息计算解析 */
    // sem_post(&g_track_process_semaphore);

 #if 0 
    sprintf(dir_name, "/home/lds/obj_data/");
    sprintf(filename, "%sobj_data_%d.bin", dir_name, s_u32frame_cnt++);
    /* 创建文件 */
    fp_t = fopen(filename, "wb");
    if (fp_t == NULL)
    {
        SET_ERROR_CODE(l_u32_ret, 0);
        goto error;
    }

    /* 写入数据 */
    fwrite(g_u8obj_info_buf, sizeof(char), sizeof(object_info_type), fp_t);

    /* 同步进内存 */
    sync();

    /* 关闭文件 */
    if (fp_t != NULL)
    {
        fclose(fp_t);
    }
#endif

    #if (0)
    /* test */
    // if (g_arm_debug_param[1] > 6)
    {
        /* 拷贝模拟数据至共享内存 */
        uint64_t l_u64_share_mem_address = 0xA9000000;
        // uint64_t l_u64_share_mem_size    = 0x01000000;    // 16M
        // uint64_t l_u64_share_mem_size    = 0x00020000;    // M
        uint64_t l_u64_share_mem_size    = 0x0c00000;    // M

        int fd = open("/dev/mem", O_RDWR | O_SYNC);
        if (fd == -1) 
        {
            perror("open");
            return 1;
        }

        // 映射文件到内存
        uint8_t * l_pu8_mmap = mmap(NULL, l_u64_share_mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, l_u64_share_mem_address);
        if (l_pu8_mmap == MAP_FAILED) 
        {
            perror("mmap");
            close(fd);
            return 1;
        }

        // 打印文件内容
        // #ifndef TEST_INPUT
        // memcpy((uint64_t *)l_pu8_mmap, &g_fram_raw_data[0], sizeof(g_fram_raw_data[0]));
        // #else
        // memcpy((uint64_t *)l_pu8_mmap, test_raw_data, sizeof(test_raw_data));
        // #endif

        FILE *fp_t;

        uint8_t *filename = "/root/xhq/fft1d_2d_20log10.bin";
        /* 创建文件 */
        fp_t = fopen(filename, "w");
        if (fp_t == NULL)
        {
            SET_ERROR_CODE(l_u32_ret, 0);
            goto error;
        }

        /* 写入数据 */
        fwrite(l_pu8_mmap, sizeof(char), l_u64_share_mem_size, fp_t);

        /* 同步进内存 */
        // sync();

        /* 关闭文件 */
        if (fp_t != NULL)
        {
            fclose(fp_t);
        }

        // 解除映射
        if (munmap(l_pu8_mmap, l_u64_share_mem_size) == -1) 
        {
            perror("munmap");
        }
        close(fd);
        //
    }
    #endif

	error:
	RETURN_ERROR_CODE(l_u32_ret, __func__);
}